package com.wzz.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzz.es.IndentEs;
import com.wzz.mapper.IndentEsMapper;
import com.wzz.pojo.Indent;
import com.wzz.service.IndentEsService;
import com.wzz.vo.PageInfoVo;
import com.wzz.vo.R;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.MatchesPattern;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author DarkClown
 * @date 2024-08-12 17:27
 * @project_name day0812-month
 * @parem
 **/
@Service
public class IndentEsServiceImpl  implements IndentEsService {
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Override
    public R selectEs(PageInfoVo pageInfoVo) {
        BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
        //模糊查询
        if (StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            queryBuilder.must(QueryBuilders.matchQuery("name",pageInfoVo.getKeyWord()));
        }
        //时间查询
        if (StringUtils.isNotEmpty(pageInfoVo.getBeginTime())&&StringUtils.isNotEmpty(pageInfoVo.getEndTime())){

            queryBuilder.must(QueryBuilders.rangeQuery("buy_time").lt(pageInfoVo.getBeginTime()).gt(pageInfoVo.getEndTime()));

        }
        Integer pageNum = 0;
        if (pageInfoVo.getPageNum()>0){
            pageNum = pageInfoVo.getPageNum() -1 ;
        }

        PageRequest pageRequest = PageRequest.of(pageNum, pageInfoVo.getPageSize());

        HighlightBuilder highlightBuilder = new HighlightBuilder();

        highlightBuilder.field("name").preTags("<font color='red'>").postTags("</font>");

        FieldSortBuilder sortBuilder = new FieldSortBuilder("buyTime");

        NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder();

        NativeSearchQueryBuilder nativeSearchQueryBuilder = searchQueryBuilder.withPageable(pageRequest)
                .withQuery(queryBuilder)
                .withHighlightBuilder(highlightBuilder)
                .withSort(sortBuilder);

        SearchHits<IndentEs> search = elasticsearchRestTemplate.search(nativeSearchQueryBuilder.build(), IndentEs.class);
        ArrayList<IndentEs> indentEs = new ArrayList<>();
        long totalHits = search.getTotalHits();
        for (SearchHit<IndentEs> searchHit : search.getSearchHits()) {
            IndentEs content = searchHit.getContent();
            if (searchHit.getHighlightField("name").size()>0&&searchHit.getHighlightField("name")!=null){

                List<String> name = searchHit.getHighlightField("name");
                String s = name.get(0);
                content.setName(s);

            }
            indentEs.add(content);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",totalHits);
        map.put("pageNum",pageInfoVo.getPageNum());
        map.put("pageSize",pageInfoVo.getPageSize());
        map.put("record",indentEs);
        return R.OK(map);
    }
}
